function previewFile() {
    var oImg = document.getElementById('oImg');
    var file = document.getElementById('ipt').files[0];
    // console.log(file)
    var reader = new FileReader();
    
    reader.addEventListener('load', function() {
        oImg.src = this.result;
        img = oImg.src;
    }, false);
    if (file) {
        reader.readAsDataURL(file);
    }
}
function previewPositive() {
    var positiveImg = document.getElementById('positiveImg');
    var file = document.getElementById('idpositive').files[0];
    // console.log(file)
    var reader = new FileReader();
    
    reader.addEventListener('load', function() {
        positiveImg.src = this.result;
        img = positiveImg.src;
    }, false);
    if (file) {
        reader.readAsDataURL(file);
    }
}
function previewFace() {
    var faceImg = document.getElementById('faceImg');
    var file = document.getElementById('idface').files[0];
    // console.log(file)
    var reader = new FileReader();
    
    reader.addEventListener('load', function() {
        faceImg.src = this.result;
        img = faceImg.src;
    }, false);
    if (file) {
        reader.readAsDataURL(file);
    }
}
window.onload = () =>{
    axios.get('/home/whoareyou/realName/getNation',{
        params:{
        }
    }).then(function(data){
        console.log(data.data.data.data)
        var list = data.data.data.data
        for(let i=1;i<=58;i++){
            document.getElementById("nationSelect").innerHTML+=`<option>${list[i]}</option>`
        }
    }).catch(function (error){
        console.log(error)
        document.getElementById("floatContent").innerHTML ="<div>"+ "很抱歉"+"</div>"+"<div>"+"请检查网络情况"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
        document.getElementById("floatContent").classList.remove("hide");
    })
    axios.get('/home/whoareyou/realName/getProvince',{
        params:{
        }
    }).then(function(data){
        console.log(data.data.data.data)
        var list = data.data.data.data
        for(let i=1;i<=34;i++){
            document.getElementById("proviceSelect").innerHTML+=`<option>${list[i]}</option>`
        }
        findmessage()
    }).catch(function (error){
        console.log(error)
        document.getElementById("floatContent").innerHTML ="<div>"+ "很抱歉"+"</div>"+"<div>"+"请检查网络情况"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
        document.getElementById("floatContent").classList.remove("hide");
    })
    function findmessage(){
        var userId = document.getElementById("who").innerHTML
        axios.get('/home/whoareyou/realName/queryRealNameByUserId',{         
            params:{
                userId:userId
            }
        }).then(function(data){
            if(data.data.data.code == 20000){
                if(data.data.data.data == null||typeof(data.data.data.data)==undefined){
    
                }else{
                    var list = data.data.data.data
                    document.getElementById("name").value = list.realName
                    var provices = document.getElementById("proviceSelect")
                    var opts=provices.getElementsByTagName("option");//得到数组option
                    for(i=0;i<opts.length;i++){
                        if(list.nativePlace == opts[i].innerHTML){
                            opts[i].selected=true;
                        }
                    }
                    var nations = document.getElementById("nationSelect")
                    var opts=nations.getElementsByTagName("option");//得到数组option
                    for(i=0;i<opts.length;i++){
                        if(list.nation == opts[i].innerHTML){
                            opts[i].selected=true;
                        }
                    }
                    document.getElementById('oImg').src =list.certificatesImg
                    document.getElementById('positiveImg').src =list.idPositive
                    document.getElementById('faceImg').src =list.idFace
                    // document.getElementById("proviceSelect").value = list.nativePlace
                    // document.getElementById("year").value = list.
                    // document.getElementById("month").value = list.
                    // document.getElementById("day").value = list.
                    document.getElementById("politics").value = list.identity
                    document.getElementById("idtype").value = list.idType
                    document.getElementById("idnum").value = list.idCard
                    document.getElementById("telephone").value = list.telephone
                    document.getElementById("email").value = list.email
                    if (list.sex == "男"){
                        document.getElementsByClassName("radio")[0].checked = "checked"
                    }else if(list.sex == "女"){
                        document.getElementsByClassName("radio")[1].checked = "checked"
                    }
                    document.getElementById("btn").onclick =function(){
                        updateRealName()
                    }
                }
            }
        }).catch(function(error){
            console.log(error)
            document.getElementById("floatContent").innerHTML = "<div>" + "出错了，请检查网络连接" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
            document.getElementById("floatContent").classList.remove("hide");
        })
    }
}
function updateRealName(){
    var name = document.getElementById("name").value
    var nation = document.getElementById("nationSelect").value
    var provice = document.getElementById("proviceSelect").value
    var year = document.getElementById("year").value
    var month = document.getElementById("month").value
    var day = document.getElementById("day").value
    var politics = document.getElementById("politics").value
    var idType = document.getElementById("idtype").value
    var idnum = document.getElementById("idnum").value
    var telephone = document.getElementById("telephone").value
    var email = document.getElementById("email").value
    var sex
    if (document.getElementsByClassName("radio")[0].checked){
        sex="男";
    }else if(document.getElementsByClassName("radio")[1].checked){
        sex="女";
    }
    var birth = year+'-'+month+'-'+day
    axios({
        url:'/home/whoareyou/realName/updateRealName',
        params:{
           email:email,
           idCard:idnum,
           idType:idType,
           identity:politics,
           nation:nation,
           nativePlace:provice,
           realName:name,
           sex:sex,
           birth:birth,
           telephone:telephone
        },
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        method:'put'
    }).then(function (data) {
        console.log(data)
        if (data.data.data.code == 20000) {
            img = document.getElementById('oImg').src
            var file = document.getElementById('ipt');
            console.log(10086)
            console.log(file.files[0]);
            var formData = new FormData()
            var userId = document.getElementById("who").innerHTML
            formData.append('multipartFile',file.files[0]);
            formData.append('myfile',file.files[0]);
            axios({
                url: `/home/whoareyou/realName/uploadCertificatesImg`,
                data: formData,
                params:{
                    userId:userId
                },
                method: 'put'
            }).then((data) => {
                console.log(data)
                if (data.data.data.code == 20000) {
                    img = document.getElementById('positiveImg').src
                    var file = document.getElementById('idpositive');
                    console.log(10086)
                    console.log(file.files[0]);
                    var formData = new FormData()
                    var userId = document.getElementById("who").innerHTML
                    formData.append('multipartFile',file.files[0]);
                    formData.append('myfile',file.files[0]);
                    axios({
                        url: `/home/whoareyou/realName/uploadIdImgPositive`,
                        data: formData,
                        params:{
                            userId:userId
                        },
                        method: 'put'
                    }).then((data) => {
                        console.log(data)
                        if (data.data.data.code == 20000) {
                            img = document.getElementById('faceImg').src
                            var file = document.getElementById('idface');
                            console.log(10086)
                            console.log(userId)
                            console.log(file.files[0]);
                            var formData = new FormData()
                            var userId = document.getElementById("who").innerHTML
                            formData.append('multipartFile',file.files[0]);
                            formData.append('myfile',file.files[0]);
                            axios({
                                url: `/home/whoareyou/realName/uploadIdImgFace`,
                                data: formData,
                                params:{
                                    userId:userId
                                },
                                method: 'put'
                            }).then((data) => {
                                document.getElementById("floatContent").innerHTML = `<div>实名认知成功</div><button onclick='closethis()' class='newbtn'>确定</button>`;
                                document.getElementById("floatContent").classList.remove("hide");   
                            }).catch((err) => {
                                console.log(err)
                            })
                        }else{
                            document.getElementById("floatContent").innerHTML = `<div>很抱歉</div><div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
                            document.getElementById("floatContent").classList.remove("hide");
                        }      
                    }).catch((err) => {
                        console.log(err)
                    })
                }else{
                    document.getElementById("floatContent").innerHTML = `<div>很抱歉</div><div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
                    document.getElementById("floatContent").classList.remove("hide");
                }   
            }).catch((err) => {
                console.log(err)
            })
        }else{
            document.getElementById("floatContent").innerHTML = `<div>很抱歉</div><div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
            document.getElementById("floatContent").classList.remove("hide");
        }
    }).catch(function (error) {
        console.log(error)
        document.getElementById("floatContent").innerHTML = "<div>" + "出错了，请检查网络连接" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
        document.getElementById("floatContent").classList.remove("hide");
    })
}
function  closethis(){
    document.getElementById("floatContent").classList.add("hide");
}
function uploadYouMes(){
    var name = document.getElementById("name").value
    var nation = document.getElementById("nationSelect").value
    var provice = document.getElementById("proviceSelect").value
    var year = document.getElementById("year").value
    var month = document.getElementById("month").value
    var day = document.getElementById("day").value
    var politics = document.getElementById("politics").value
    var idType = document.getElementById("idtype").value
    var idnum = document.getElementById("idnum").value
    var telephone = document.getElementById("telephone").value
    var email = document.getElementById("email").value
    var sex
    if (document.getElementsByClassName("radio")[0].checked){
        sex="男";
    }else if(document.getElementsByClassName("radio")[1].checked){
        sex="女";
    }
    var birth = year+'-'+month+'-'+day
    console.log(birth)
    axios.post('/home/whoareyou/realName/addRealName',{
        params:{
           email:email,
           idCard:idnum,
           idType:idType,
           identity:politics,
           nation:nation,
           nativePlace:provice,
           realName:name,
           sex:sex,
           birth:birth,
           telephone:telephone
        }
    }).then(function (data) {
        if (data.data.data.code == 20000) {
            img = document.getElementById('oImg').src
            var file = document.getElementById('ipt');
            console.log(10086)
            console.log(file.files[0]);
            var formData = new FormData()
            var userId = document.getElementById("who").innerHTML
            formData.append('multipartFile',file.files[0]);
            formData.append('myfile',file.files[0]);
            axios({
                url: `/home/whoareyou/realName/uploadCertificatesImg`,
                data: formData,
                params:{
                    userId:userId
                },
                method: 'put'
            }).then((data) => {
                console.log(data)
                if (data.data.data.code == 20000) {
                    img = document.getElementById('positiveImg').src
                    var file = document.getElementById('idpositive');
                    console.log(10086)
                    console.log(file.files[0]);
                    var formData = new FormData()
                    var userId = document.getElementById("who").innerHTML
                    formData.append('multipartFile',file.files[0]);
                    formData.append('myfile',file.files[0]);
                    axios({
                        url: `/home/whoareyou/realName/uploadIdImgPositive`,
                        data: formData,
                        params:{
                            userId:userId
                        },
                        method: 'put'
                    }).then((data) => {
                        console.log(data)
                        if (data.data.data.code == 20000) {
                            img = document.getElementById('faceImg').src
                            var file = document.getElementById('idface');
                            console.log(10086)
                            console.log(userId)
                            console.log(file.files[0]);
                            var formData = new FormData()
                            var userId = document.getElementById("who").innerHTML
                            formData.append('multipartFile',file.files[0]);
                            formData.append('myfile',file.files[0]);
                            axios({
                                url: `/home/whoareyou/realName/uploadIdImgFace`,
                                data: formData,
                                params:{
                                    userId:userId
                                },
                                method: 'put'
                            }).then((data) => {
                                document.getElementById("floatContent").innerHTML = `<div>提交成功，请等待管理员审核</div><button onclick='closethis()' class='newbtn'>确定</button>`;
                                document.getElementById("floatContent").classList.remove("hide");   
                            }).catch((err) => {
                                console.log(err)
                            })
                        }else{
                            document.getElementById("floatContent").innerHTML = `<div>很抱歉</div><div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
                            document.getElementById("floatContent").classList.remove("hide");
                        }      
                    }).catch((err) => {
                        console.log(err)
                    })
                }else{
                    document.getElementById("floatContent").innerHTML = `<div>很抱歉</div><div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
                    document.getElementById("floatContent").classList.remove("hide");
                }   
            }).catch((err) => {
                console.log(err)
            })
        }else{
            document.getElementById("floatContent").innerHTML = `<div>很抱歉</div><div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
            document.getElementById("floatContent").classList.remove("hide");
        }
    }).catch(function (error) {
        console.log(error)
        document.getElementById("floatContent").innerHTML = "<div>" + "出错了，请检查网络连接" + "</div>" + "<button onclick='closethis()' class='newbtn'>" + "确定" + "</button>";
        document.getElementById("floatContent").classList.remove("hide");
    })
}